package com.louis.buddhist.widget.dialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.louis.buddhist.R;
import com.louis.buddhist.data.C;
import com.louis.buddhist.data.entity.Book;
import com.louis.buddhist.data.entity.CollectEntity;
import com.louis.buddhist.db.BookDaoOpen;
import com.louis.buddhist.db.CollectDaoOpen;
import com.louis.buddhist.interfaces.ReadCloseActivityListener;
import com.louis.buddhist.ui.activity.ReadActivity;
import com.louis.buddhist.ui.adapter.CollectDialogAdapter;

import java.util.List;

public class CollectDialogFragment extends DialogFragment {


    private static final String TAG = "CollectDialogFragment";
    private View mView;
    private RecyclerView mRecyclerView;
    private List<CollectEntity> mCollectEntities;
    private CollectDialogAdapter mAdapter;
    private ReadCloseActivityListener mListener;

    public void setmListener(ReadCloseActivityListener mListener) {
        this.mListener = mListener;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.dialog_collect, container, false);
        //启用窗体的扩展特性。
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getDialog().setCanceledOnTouchOutside(false);
        getDialog().setCancelable(false);
        getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return true;
                }
                return false;
            }
        });
        initView();
        return mView;
    }

    private void initView() {

        ImageView close = mView.findViewById(R.id.iv_close);
        mRecyclerView = mView.findViewById(R.id.rv_list);

        initRecyclerView();

        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    private void initRecyclerView() {

        mCollectEntities = CollectDaoOpen.queryAll(getActivity());
        if (mCollectEntities != null && mCollectEntities.size() > 0) {
            mAdapter = new CollectDialogAdapter(getActivity(), R.layout.adapter_collect_list, mCollectEntities);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());

            mRecyclerView.setLayoutManager(linearLayoutManager);
            mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                @Override
                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                    CollectEntity entity = (CollectEntity) adapter.getData().get(position);
                    switch (view.getId()){
                        case R.id.tv_read:
                            read(entity.getName(), entity.getPage());
                            if(mListener != null){
                                mListener.close();
                            }
                            break;
                        case R.id.tv_del:
                            del(entity.getId());
                            break;
                    }
                }
            });
            mRecyclerView.setAdapter(mAdapter);
        }
    }

    private void del(Long id) {
        CollectDaoOpen.deleteByKeyData(getActivity(), id);
        mCollectEntities.clear();
        mCollectEntities.addAll(CollectDaoOpen.queryAll(getActivity()));
        mAdapter.notifyDataSetChanged();
    }

    private void read(String name, int page) {
        Book book = BookDaoOpen.queryByName(getActivity(), name);
        if(book != null){
            Intent intent = new Intent(getActivity(), ReadActivity.class);
            intent.putExtra(C.IntentKey.MESSAGE_INTENT_KEY, book);
            intent.putExtra(C.IntentKey.MESSAGE_INTENT_KEY2, page - 1);
            startActivity(intent);
            dismiss();
        }else{
            Toast.makeText(getActivity(), name + "沒有下載或已刪除！", Toast.LENGTH_SHORT).show();
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return super.onCreateDialog(savedInstanceState);
    }
}